﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;

namespace RCU_Trial
{
    
    class Program
    {
        /*stuff for receiving*/
        static int listenPort = 11000;

        static void Main(string[] args)
        {
            #region argument parsing
            /*parse arguments*/
            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    string[] parse = args[i].Split('-');
                    if(parse.Length != 2){ throw new ArgumentException("Error parsing argument: " + args[i]); }
                    else
                    {
                        switch (parse[0])
                        {
                            case "listenport":
                                listenPort = Int32.Parse(parse[1]);
                                Console.WriteLine(">Listening port changed to " + listenPort);
                                break;
                        }
                    }
                }
            }
            catch(Exception Ex)
            {
                Console.WriteLine(Ex.ToString());
                return;
            }
            #endregion
            /*start receiver*/
            Receiver receiver = new Receiver(listenPort);
            Thread receiverThread = new Thread(new ThreadStart(receiver.Receive));
            receiverThread.Start();

            /*monitor threads*/
            while (true)
            {
                if (!receiverThread.IsAlive)
                {
                    //receiver has died!
                    Console.WriteLine("Detected dead receiver. Restarting...");
                    receiver = new Receiver();
                    receiverThread = new Thread(new ThreadStart(receiver.Receive));
                    receiverThread.Start();
                }
            }
        }

    }
}
